<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="components/easyui/easyui-list :: head('数据表管理', 'easyui')">
<body>
<div id="tb">
    表名: <input class="easyui-textbox" style="width: 160px" id="tableName" name="tableName" data-options="required:false">
    表标注: <input class="easyui-textbox" style="width: 160px" id="tableComment" name="tableComment" data-options="required:false">
    <a href="javascript:void(0)" class="easyui-linkbutton c-primary" style="width:80px" onclick="doSearch()"><i class="fa fa-search" aria-hidden="true"></i> <span th:text="#{search}"></span></a>
    <a href="javascript:void(0)" class="easyui-linkbutton c-basic" style="width:80px" onclick="doSearchReset()"><i class="fa fa-repeat" aria-hidden="true"></i> <span th:text="#{reset}"></span></a>
</div>
<table id="tt" data-options="url: 'tables/data', method:'get',fit:true,rownumbers:true,pagination:true,pageSize:10,toolbar:'#tb',idField:'tableName', singleSelect: true, selectOnCheck: true, checkOnSelect: true">
    <thead>
    <tr>
        <th data-options="field:'ck',checkbox:true"></th>
        <th field="tableName" width="160">表名</th>
        <th field="tableComment" width="160">表标注</th>
        <th field="entityName" width="160">实体名</th>
        <th data-options="field:'operation',width:160,formatter:formatGen">操作</th>
    </tr>
    </thead>
</table>

<div id="generateCodeModal" class="easyui-window" title="代码生成" data-options="modal:true,closed:true,collapsible:false" style="width:640px;height:320px;padding:10px;">
    <form id="ff" method="post">
        <div>
            <input id="gen_code_table_name" class="easyui-textbox" name="tableName" style="width:100%" data-options="label:'表名:',required:true,readonly:true">
        </div>
        <div>
            <input id="gen_code_entity_name" class="easyui-textbox" name="entityName" style="width:100%" data-options="label:'实体类名:',required:true">
        </div>
        <div>
            <select id="gen_code_id_type" class="easyui-combobox" name="idType" style="width:100%;" data-options="label:'id列类型:', required:true">
                <option value="Long">Long</option>
                <option value="String">String</option>
            </select>
        </div>
        <div style="margin-bottom:20px">
            <label>要生成的代码:</label>
            <label>
                <input type="checkbox" name="genModules" value="modelAndMapper">modelAndMapper
            </label>
            <label>
                <input type="checkbox" name="genModules" value="repository">repository
            </label>
            <label>
                <input type="checkbox" name="genModules" value="service">service
            </label>
            <label>
                <input type="checkbox" name="genModules" value="controller">controller
            </label>
            <label>
                <input type="checkbox" name="genModules" value="controllerAndPage">controllerAndPage
            </label>
        </div>
        <div style="text-align:center;padding:5px 0">
            <a href="javascript:void(0)" class="easyui-linkbutton c-primary" style="width:80px" onclick="generateCode()">生成</a>
            <a href="javascript:void(0)" class="easyui-linkbutton c-basic" style="width:80px" onclick="clearForm()" th:text="#{cancel}"></a>
        </div>
    </form>
</div>
<div th:replace="components/easyui/easyui-list :: js('easyui,layer')"></div>
<script>
    function showWindow(options) {
        layer.open({
            type: 2,
            title: options.title,
            shadeClose: true,
            shade: 0.6,
            maxmin: true, //开启最大化最小化按钮
            area: ['893px', '600px'],
            content: options.content
        });
    }

    function toastInfo(options) {
        toastr[options.type](options.content, options.title);
    }

    function doSearch() {
        console.log("doSearch");
        $('#tt').datagrid('load', {
            tableName: $('#tableName').val(),
            tableComment: $('#tableComment').val()
        });
    }

    function doSearchReset(){
        $('#tableName').textbox('setValue', '');
        $('#tableComment').textbox('setValue', '');
        doSearch();
    }

    function formatGen(val,row){
        return '<a href="javascript:void(0)" class="button button-rounded button-small button-primary button-pill" onclick="openGenerateCodeModal(\''
                + row.tableName + '\',\''
                + row.entityName + '\',\''
                + row.tableComment + '\',\''
            + '\')">代码生成</a>';
    }

    function openGenerateCodeModal(tableName, entityName, tableComment) {
        /*console.log(tableName + ',' + entityName);
        $('#gen_code_table_name').textbox('setValue', tableName);
        $('#gen_code_entity_name').textbox('setValue', entityName);
        $('#gen_code_id_type').combobox('setValue', 'Long');
        $('#generateCodeModal').window('open');*/
        /*layer.open({
            type: 2,
            title: '代码生成',
            shadeClose: true,
            shade: false,
            maxmin: true, //开启最大化最小化按钮
            area: ['640px', '320px'],
            content: 'tables/codeGenerate?tableName=' + tableName + '&entityName=' + entityName + '&tableComment=' + tableComment
        });*/
        showWindow({
            title:'代码生成',
            content:'/tables/codeGenerate?tableName=' + tableName + '&entityName=' + entityName + '&tableComment=' + tableComment
        });
    }

    function submitForm(){
        $('#ff').form('submit');
    }
    function clearForm(){
        $('#ff').form('clear');
        $('#generateCodeModal').window('close');
    }

    function generateCode() {
        let tableName = $('#generateCodeModal input[name="tableName"]').val();
        let entityName = $('#generateCodeModal input[name="entityName"]').val();
        let idType = $('#generateCodeModal input[name="idType"]').val();
        let genModules = $("#generateCodeModal input[name='genModules']:checked");
        let checkBoxValue = "";
        genModules.each(function(){
            checkBoxValue += $(this).val()+",";
        });
        checkBoxValue = checkBoxValue.substring(0,checkBoxValue.length-1);
        console.log(checkBoxValue);
        $.ajax({
            type: 'GET',
            url: 'tables/generate?tableName=' + tableName + '&entityName=' + entityName + '&genModules=' + checkBoxValue + '&idType=' + idType,
            dataType: 'json',
            beforeSend: function () {
                $.messager.progress({
                   title: '提示信息',
                    msg: '代码生成中，请稍候......'
                });
            },
            complete: function () {
                $.messager.progress('close');
            },
            success: function (data) {
                console.log(data);
                if(data.code == 200){
                    $.messager.show({
                        title:'提示信息',
                        msg:'代码生成成功！',
                        timeout:5000,
                        showType:'slide'
                    });
                }
            }
        });
    }
</script>
</body>
</html>